#!usr/bin/env python
# -*- coding:utf-8 _*-
"""
@author:zhuyan
@file: domxmlutil.py
@time: 2018/08/20
"""

# !/usr/bin/python
# coding=utf8
from xml.dom import minidom
import os


class ChildIndexOutofBoundsException(Exception):
    pass


class DOMXmlUtil:
    def __init__(self):
        self.dom = None
        self.root = None
        pass

    def readXmlString(self, data):
        self.dom = minidom.parseString(data, "UTF-8")
        self.root = self.dom.firstChild
        return self.root

    def readXmlFile(self, fp):
        self.dom = minidom.parse(fp)
        self.root = self.dom.documentElement
        print(self.root.toxml())
        return self.root

    def getRootElement(self):
        return self.root


class XmlNode:
    def __init__(self, elem):
        self.elem = elem

    def getChildByIndex(self, index):
        children = self.elem.childNodes
        children_elem = []
        for child in children:
            if child.nodeType == minidom.Node.ELEMENT_NODE:
                children_elem.append(child)
        if index > len(children_elem):
            raise ChildIndexOutofBoundsException
        return XmlNode(children_elem[index])

    def getChildByName(self, name):
        children = self.elem.childNodes
        for child in children:
            if child.nodeType == minidom.Node.ELEMENT_NODE:
                if child.nodeName == name:
                    return XmlNode(child)
        return None

    def getAttributeByIndex(self, index):
        attributes = self.elem.attributes
        keys = attributes.keys()
        if index > len(keys):
            raise ChildIndexOutofBoundsException
        return Attribute(keys[index], attributes.getNamedItem(keys[index]).value)

    def getAttributeByName(self, name):
        attributes = self.elem.attributes
        keys = attributes.keys()
        for each in keys:
            if each == name:
                return Attribute(name, attributes.getNamedItem(name).value)
        return None

    def getName(self):
        return self.elem.nodeName

    def getValue(self):
        if self.elem.childNodes.length == 0:
            return ""
        elif self.elem.childNodes.length == 1:
            return self.elem.firstChild.nodeValue
        else:
            return None

    def getChildList(self):
        children = self.elem.childNodes
        childList = []
        for child in children:
            if child.nodeType == minidom.Node.ELEMENT_NODE:
                childList.append(XmlNode(child))
        return childList

    def getAttributeList(self):
        attributes = self.elem.attributes
        attrList = []
        keys = attributes.keys()
        for key in keys:
            attrList.append(Attribute(key, attributes.getNamedItem(key).value))
        return attrList


class Attribute:
    def __init__(self, name, value):
        self.name = name
        self.value = value

    def getName(self):
        return self.name

    def getValue(self):
        return self.value


if __name__ == '__main__':
    util = DOMXmlUtil()
    print(os.getcwd())
    fp = open(os.getcwd()+"/dt.xml", encoding='utf-8', mode='a+')
    root = util.readXmlFile(fp)

    xml_node = XmlNode(root)
    print(xml_node.getChildByName("sec11").getValue())

